package com.iwhalecloud.uncc.mq.handler;

import com.iwhalecloud.uncc.common.SpringContext;
import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.domain.ArchiverMsg;
import com.iwhalecloud.uncc.domain.InsCollectServiceArchiverMsg;
import com.iwhalecloud.uncc.mq.listener.IMqHandle;
import com.iwhalecloud.uncc.utils.ArchiverUtil;
import com.iwhalecloud.uncc.utils.CheckUtil;
import com.iwhalecloud.uncc.utils.HisUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * @author zq
 * @date 2021/10/20 19:15
 * @description InsCollectServiceArchiverHandler
 */
@Slf4j
public class InsCollectServiceArchiverHandler implements IMqHandle , ArchiverHandler{

    private ArchiverDao insAlarmMsgArchiverDao;
    private ArchiverDao insServiceActiveLogArchiverDao;
    private ArchiverDao moniBrStatInfoArchiverDao;
    private ArchiverDao moniItStatInfoArchiverDao;
    private ArchiverDao scalServiceExpanseLogArchiverDao;
    private ArchiverDao insEventRecordArchiverDao;

    @Override
    public void init(Map<String, Object> params) throws Exception {
        insAlarmMsgArchiverDao = (ArchiverDao) getBean("insAlarmMsgArchiverDao");
        insServiceActiveLogArchiverDao = (ArchiverDao) getBean("insServiceActiveLogArchiverDao");
        moniBrStatInfoArchiverDao = (ArchiverDao) getBean("moniBrStatInfoArchiverDao");
        moniItStatInfoArchiverDao = (ArchiverDao) getBean("moniItStatInfoArchiverDao");
        scalServiceExpanseLogArchiverDao = (ArchiverDao) getBean("scalServiceExpanseLogArchiverDao");
        insEventRecordArchiverDao = (ArchiverDao) getBean("insEventRecordArchiverDao");
    }

    private Object getBean(String beanName) {
        return SpringContext.getContext().getBean(beanName);
    }

    @Override
    public boolean dealMessage(List messages) {

        long s = System.currentTimeMillis();
        if (log.isInfoEnabled()) {
            log.info(Thread.currentThread().getName()+ "-InsCollectServiceArchiverHandler开始消费"+messages.size()+"条InsCollectServiceArchiverMsg");
        }

        ArchiverUtil.dealMessage(messages,InsCollectServiceArchiverMsg.class,this);

        if (log.isInfoEnabled()) {
            log.info(Thread.currentThread().getName() + "消费完,耗时:" + ((System.currentTimeMillis() - s) / 1000) + "s");
        }

        return true;
    }

    @Override
    public void dealArchiverMsg(ArchiverMsg msg){
        this.dealArchiverMsg((InsCollectServiceArchiverMsg)msg);
    }

    private void dealArchiverMsg(InsCollectServiceArchiverMsg msg) {
        if(msg.getInsCollectServiceList() == null){
            return;
        }
        doArchive(msg);
        isDeleteOnline(msg);

    }

    private void doArchive(InsCollectServiceArchiverMsg msg) {
        String newPartitionCode = HisUtil.changeHisDatasource(msg.getPartitionCode());

        CheckUtil.batchInsert(insAlarmMsgArchiverDao,msg.getInsAlarmMsgList(),newPartitionCode);
        CheckUtil.batchInsert(insServiceActiveLogArchiverDao,msg.getInsServiceActiveLogList(),newPartitionCode);
        CheckUtil.batchInsert(moniBrStatInfoArchiverDao,msg.getMoniBrStatInfoList(),newPartitionCode);
        CheckUtil.batchInsert(moniItStatInfoArchiverDao,msg.getMoniItStatInfoList(),newPartitionCode);
        CheckUtil.batchInsert(scalServiceExpanseLogArchiverDao,msg.getScalServiceExpanseLogList(),newPartitionCode);
        CheckUtil.batchInsert(insEventRecordArchiverDao,msg.getInsEventRecordList(),newPartitionCode);
    }

    private void isDeleteOnline(InsCollectServiceArchiverMsg msg) {
        //切换oldPartitionCode 修改删除
        DatasourceContextHolder.setDs(msg.getPartitionCode());
        if(msg.getIsDeleteOnline()){
            CheckUtil.batchDelete(insAlarmMsgArchiverDao,msg.getInsAlarmMsgList());
            CheckUtil.batchDelete(insServiceActiveLogArchiverDao,msg.getInsServiceActiveLogList());
            CheckUtil.batchDelete(moniBrStatInfoArchiverDao,msg.getMoniBrStatInfoList());
            CheckUtil.batchDelete(moniItStatInfoArchiverDao,msg.getMoniItStatInfoList());
            CheckUtil.batchDelete(scalServiceExpanseLogArchiverDao,msg.getScalServiceExpanseLogList());
            CheckUtil.batchDelete(insEventRecordArchiverDao,msg.getInsEventRecordList());
        }else{
            CheckUtil.batchUpdateArchived(insAlarmMsgArchiverDao,msg.getInsAlarmMsgList());
            CheckUtil.batchUpdateArchived(insServiceActiveLogArchiverDao,msg.getInsServiceActiveLogList());
            CheckUtil.batchUpdateArchived(moniBrStatInfoArchiverDao,msg.getMoniBrStatInfoList());
            CheckUtil.batchUpdateArchived(moniItStatInfoArchiverDao,msg.getMoniItStatInfoList());
            CheckUtil.batchUpdateArchived(scalServiceExpanseLogArchiverDao,msg.getScalServiceExpanseLogList());
            CheckUtil.batchUpdateArchived(insEventRecordArchiverDao,msg.getInsEventRecordList());
        }
    }


//    private void batch(String k, List v , boolean f , ArchiverDao archiverDao) {
//        String oldPartitionCode = k;
//        int hisNum = PartitionUtil.getHisNum(oldPartitionCode, archiverProperties.getHistoryDbNum());
//        if(oldPartitionCode != null) {
//            DatasourceContextHolder.setDs(ArchiverConstant.HIS+hisNum);
//        }else{
//            DatasourceContextHolder.setDs(ArchiverConstant.DB_DEFAULT_HIS);
//        }
//        String newPartitionCode = PartitionUtil.getPartitionCode(oldPartitionCode, archiverProperties.getHistoryDbNum());
////        batchInsert(archiverDao,v,newPartitionCode);
//        if(v !=null && !v.isEmpty()){
//            for (Object o : v) {
//                ArchiverModel a= (ArchiverModel)o;
//                a.setPartitionCode(newPartitionCode);
//                a.setArchiverTime(Date.from(Instant.now()));
//            }
//            archiverDao.batchInsert(v);
//        }
//
//        //切换oldPartitionCode 修改删除
//        DatasourceContextHolder.setDs(oldPartitionCode);
//        if(f){
////            batchDelete(archiverDao,v);
//            if(v !=null && !v.isEmpty()){
//                archiverDao.batchDelete(v);
//            }
//        }else{
////            batchUpdate(archiverDao,v);
//            if(v !=null && !v.isEmpty()){
//                archiverDao.batchUpdate(ArchiverConstant.ARCHIVED,v);
//            }
//        }
//    }
}
